package com.mini6410.PWM;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;

import com.friendlyarm.AndroidSDK.HardwareControler;
import com.mini6410.R;

/**
 * 
 * ClassName:PWMActivity
 * Reason:	 PWM Demo
 *
 * @author   snowdream
 * @version  
 * @since    Ver 1.1
 * @Date	 2011	2012-03-11		17:21
 *
 * @see 	 
 */
public class PWMActivity extends Activity implements Button.OnClickListener,ToggleButton.OnCheckedChangeListener{
	public static final String TAG = "PWMActivity";

	/*PWM频率*/
	public static final int PWM_FREQUENCY_1 = 1;
	public static final int PWM_FREQUENCY_100 = 100;
	public static final int PWM_FREQUENCY_200 = 200;
	public static final int PWM_FREQUENCY_500 = 500;
	public static final int PWM_FREQUENCY_1000 = 1000;
	public static final int PWM_FREQUENCY_2000 = 2000;
	public static final int PWM_FREQUENCY_5000 = 5000;
	public static final int PWM_FREQUENCY_10000 = 10000;

	/*PWM播放时长*/
	public static final int PWM_PLAY_PERIOD_FOR_EVER = -1;
	public static final int PWM_PLAY_PERIOD_100 = 100;
	public static final int PWM_PLAY_PERIOD_200 = 200;
	public static final int PWM_PLAY_PERIOD_300 = 300;
	public static final int PWM_PLAY_PERIOD_500 = 500;
	public static final int PWM_PLAY_PERIOD_1000 = 1000;

	/*PWM播放间隔*/
	public static final int PWM_WAIT_PERIOD_100 = 100;
	public static final int PWM_WAIT_PERIOD_200 = 200;
	public static final int PWM_WAIT_PERIOD_300 = 300;
	public static final int PWM_WAIT_PERIOD_500 = 500;
	public static final int PWM_WAIT_PERIOD_1000 = 1000;


	/*播放频率*/
	public int mfrequency = PWM_FREQUENCY_1000;
	
	/*频率改变步长Step*/
	public int mfrequencystep = 100;
	
	/*播放时长*/
	public int mplayperiod = PWM_PLAY_PERIOD_1000;
	
	/*播放间隔*/
	public int mwaitperiod = PWM_WAIT_PERIOD_1000;
	
	/*播放次数*/
	public int mplaynum = 20;

	private boolean mStop = false;

	private Button mButtonSub = null;
	private Button mButtonAdd = null;

	private ToggleButton mToggleButtonStartForEver= null;
	private ToggleButton mToggleButtonStartForTimes = null;

	private EditText mEditTextFrequency = null;
	private EditText mEditTextPlayPeriod = null;
	private EditText mTextViewWaitPeriod = null;
	private EditText mEditTextPlayNum = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pwmdemo);

		initUI();
		initData();

	}

	@Override
	protected void onDestroy() {
		mStop = true;
		super.onDestroy();
	}

	/**
	 * 
	 * initUI: 初始化UI
	 *
	 * @param   
	 * @return     
	 * @throws 
	 */
	public void initUI(){
		mButtonSub = (Button)findViewById(R.id.ButtonSUB);
		mButtonAdd = (Button)findViewById(R.id.ButtonADD);

		/*按钮监听器，具体实现参考下面的函数：onClick*/
		mButtonSub.setOnClickListener(this);
		mButtonAdd.setOnClickListener(this);

		mToggleButtonStartForEver= (ToggleButton)findViewById(R.id.ToggleButtonStartForEver);
		mToggleButtonStartForTimes = (ToggleButton)findViewById(R.id.ToggleButtonStartForTimes);

		/*开关按钮监听器，具体实现参考下面的函数：onCheckedChanged*/
		mToggleButtonStartForEver.setOnCheckedChangeListener(this);
		mToggleButtonStartForTimes.setOnCheckedChangeListener(this);

		mEditTextFrequency = (EditText)findViewById(R.id.EditTextFrequency);
		mEditTextPlayPeriod = (EditText)findViewById(R.id.EditTextPlayPeriod);
		mTextViewWaitPeriod = (EditText)findViewById(R.id.EditTextWaitPeriod);
		mEditTextPlayNum = (EditText)findViewById(R.id.EditTextPlayNum);

		/*EditText监听器，具体实现参考下面的函数：onTextChanged
		 * 主要用于获取更改后最新的数值，下同。
		 * */
		mEditTextFrequency.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					mfrequency = Integer.parseInt(mEditTextFrequency.getText().toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		mEditTextPlayPeriod.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					mplayperiod = Integer.parseInt(mEditTextPlayPeriod.getText().toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		mTextViewWaitPeriod.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					mwaitperiod = Integer.parseInt(mTextViewWaitPeriod.getText().toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		mEditTextPlayNum.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					mplaynum = Integer.parseInt(mEditTextPlayNum.getText().toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});


	}

	/**
	 * 
	 * initData: 初始化各个控件的数值
	 *
	 * @param   
	 * @return     
	 * @throws 
	 */
	public void initData(){
		mEditTextFrequency.setText(String.valueOf(mfrequency));
		mEditTextPlayPeriod.setText(String.valueOf(mplayperiod));
		mTextViewWaitPeriod.setText(String.valueOf(mwaitperiod));
		mEditTextPlayNum.setText(String.valueOf(mplaynum));
	}


	/**
	 * 
	 * onCheckedChanged: 开关按钮监听器。当有开关按钮的状态改变时，响应点击。
	 *
	 * @param   buttonView 改变状态的开关按钮对象；
	 * @param   isChecked  该开关按钮是否被选中；
	 * @return     
	 * @throws 
	 */
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		ToggleButton mToggleButton = (ToggleButton)buttonView;

		switch (mToggleButton.getId()) {
		case R.id.ToggleButtonStartForEver:
			if(isChecked){
				PWMPlay(mfrequency);
			}else{
				mStop = true;
				PWMStop();
			}
			break;
		case R.id.ToggleButtonStartForTimes:
			if(isChecked){
				mStop = false;
				PWMThread mPWMThread = new PWMThread();
				mPWMThread.start();
			}else{
				mStop = true;
			}
			break;		
		default:
			break;
		}
	}

	/**
	 * 
	 * onClick: 按钮监听器。当有按钮被点击时，响应点击。
	 *
	 * @param   v 被点击的按钮对象；
	 * @return     
	 * @throws 
	 */
	public void onClick(View v) {
		Button mButton = (Button)v;

		switch (mButton.getId()) {
		case R.id.ButtonSUB:
			if(mfrequency <= 1)
				mfrequency = PWM_FREQUENCY_1;
			else if(mfrequency == PWM_FREQUENCY_100)
				mfrequency = PWM_FREQUENCY_1;	
			else
				mfrequency -= mfrequencystep;

			mEditTextFrequency.setText(String.valueOf(mfrequency));
			break;
		case R.id.ButtonADD:
			if(mfrequency <= 1)
				mfrequency = PWM_FREQUENCY_100;
			else
				mfrequency += mfrequencystep;

			mEditTextFrequency.setText(String.valueOf(mfrequency));
			break;
		default:
			break;
		}
	}

	/**
	 * 
	 * PWMPlay: 启动蜂鸣器 ，并播放。
	 *
	 * @param   frequency 播放频率
	 * @return  true 表示播放成功；否则，表示播放失败。  
	 * @throws 
	 */
	public boolean PWMPlay(int frequency)
	{
		boolean ret  = false;
		int result = -1;

		result = HardwareControler.PWMPlay(frequency);

		if(result == 0)
			ret = true;
		else
			ret = false;

		return ret;
	}

	/**
	 * 
	 * PWMStop: 停止并关闭蜂鸣器。
	 *
	 * @param   
	 * @return  true 表示停止成功；否则，表示停止失败。  
	 * @throws 
	 */
	public boolean PWMStop(){
		boolean ret  = false;
		int result = -1;

		result = HardwareControler.PWMStop();

		if(result == 0)
			ret = true;
		else
			ret = false;

		return ret;		
	}

	/**
	 * 
	 * PWMThread: PWM播放线程
	 *
	 * @param   
	 * @return   
	 * @throws 
	 */
	public class PWMThread extends Thread{

		@Override
		public void run() {
			Log.i(TAG, "PWMThread Start");

			for(int i = 0 ; i < mplaynum; i++ )
			{
				Log.i(TAG, String.valueOf(i));

				if(mStop)
				{
					PWMStop();
					break;
				}

				/*启动蜂鸣器，并使用频率 mfrequency 进行播放*/
				PWMPlay(mfrequency);

				/*等待播放时长 mplayperiod 结束*/
				try {
					sleep(mplayperiod);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				/*停止蜂鸣器*/
				PWMStop();

				if(mStop)
					break;

				/*等待播放间隔 mwaitperiod 结束*/
				try {
					sleep(mwaitperiod);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				

			}
			Log.i(TAG, "PWMThread Stop");
		}
	}
}
